#ifndef __FAULT_CODE_H
#define __FAULT_CODE_H    

#ifdef __cplusplus
 extern "C" {
#endif
     

#include "math.h"
#include "sys.h"
#include "encoder.h"
#include "timer.h"
#include "OpenImu.h"
#include "WingbowHost.h"  


extern u8  SpiTimeOut;   //Imu通信超时故障
extern OpenImu *openImu;


#define  Ledlen   8

#define  PriImu         A
#define  PriEncoder     B
#define  PriUart        C


//Led ID越小优先级越高
typedef struct {
    u16                     Led[Ledlen];            //一次循环中电机错误个数计数
    u16                     FaultLedDeep[Ledlen];   //表示故障严重程度 
}FaultLed_T;


enum FaultSensorPriority{
    A=0x00,
    B=0x01,
    C,
    D,
    E,
    F,
    G,
    H     
};

enum FaultDeep{
    Good=0x00,
    Slight=0x01,
    Medium,
    Bad
};

enum SensorErrType{
  NoErr      =0x00,          //无错误
  DataErr    =0x01,          //数据异常无数据
  Higher     =0x02,          //高度超过阈值
  Lower      =0x03,          //高度低于阈值
  TimeOut    =0x04,          //通信超时
  IdErr      =0x05,          //Id错误

};  

typedef struct SensorDevice{
    unsigned char               err_status;  //模块当前所有错误状态按位与                      
    unsigned int                err_cnt;     //模块发生的错误计数
    enum SensorErrType          now_err;     //超模块当前错误类型
}SensorFault;

typedef struct {
    unsigned char               FaultSensorCnt;   //故障传感器个数
    unsigned char               TimeOutFlag;    
}SensorServe;

typedef struct EncoderSensorWingbot{
    SensorFault            EncoderSensor[4];   //4个编码器
    SensorServe            FaultSensor;        //记录故障传感器数据的结构体 
}EncoderWingbot_T;                             //编码器故障结构体


typedef struct ImuSensorWingbot{
    SensorFault     Imu[4];             //4款Imu  
    SensorServe     FaultSensor;        //记录故障传感器数据的结构体 
}ImuWingbot_T;                          //Imu故障结构体


typedef struct UartDeviceWingbot{
    SensorFault     Uart[4];             //4款Imu  
    SensorServe     FaultSensor;        //记录故障传感器数据的结构体 
}UartWingbot_T; 

typedef struct FaultDevice{
    EncoderWingbot_T          EncoderWingbot;
    ImuWingbot_T              ImuWingbot;
    UartWingbot_T             UartWingbot;
    FaultLed_T                FaultLed;
}FaultDeviceWingbot_T;                  //AGV F1整车故障结构体


enum PanasonicMotorErrType{
  M_NoErr       =0x00,          //无错误  
  TmeoutErr   =0x01,          //数据超时
  OverLoadErr =0x02,          //过载警告
  OverregenerativeErr=0x03,   //过再生警告
  BatteryErr  =0x04,          //电池警告
  FanErr,                     //风扇警告
  EncoderCommunicateErr,      //编码器通信警告
  EncoderHeatingErr,          //编码器过热警告
  GenerateVibrationDetectionErr,      //发振检出警告
  LifeDetectionErr,            //寿命检出警告
  DisplacementFaultErr,        //外部位移传感器异常警告  
  DisplacementCommunicateErr,  //外部位移传感器通信警告
  DegradationDiagnosisErr,     //劣化诊断警告
  MainPowerOffErr,             //主电源关闭警告
  OtherErr                     //其他警告
};  

u8 CheckImuTimeOut(OpenImu *Adis);
u8 CheckImuFault(OpenImu *Adis);
u8 CheckEncoderFault(Encoder* encoder_t);
u8 CheckUartFault();
u8 UpdateFaultDepp();                    //更新故障深度
u8 UpdateImuFaultDepp();                 //更新IMU故障
u8 UpdateEncoderFaultDepp();             //更新ENCODER故障
u8 UpdateUartFaultDepp();                //更新串口故障程度
void loop20ms();                         //20ms循环
void loop200ms();                        //200ms循环
void mainloop();                         //主循环


u8 FaultLedLink();
u8 StatusLedLink();

extern FaultDeviceWingbot_T WingbotFault;

#ifdef __cplusplus
 }
#endif
#endif

